/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MainGuiForm.java
 *
 * Created on 12.01.2010., 02.19.16
 */

package gui;

import client.Requests;
import client.Task;
import java.util.List;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import javax.swing.JFrame;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JLabel;

/**
 *
 * @author Runner
 */

public class MainGui {

    final public Integer memberId;
    final public Requests req;

    public JFrame f;
    public JButton jButtonRefresh;
    public JButton jButtonAddTask;
    public JButton jButtonNewMember;
    public JCheckBox jCheckBox;
    public JPanel jPanel1;
    public JPanel jPanelCheckBox;

    public MainGui(Integer memberId, Requests req) throws Exception {
        
        this.memberId = memberId;
        this.req = req;

        f = new JFrame("Collab - Tasks");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setBounds(new Rectangle(450, 250, 400, 400));
        f.setResizable(false);

        jPanel1 = new JPanel();
        jPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));

        jButtonRefresh = new JButton();
        jButtonRefresh.setIcon(new ImageIcon("/xml-collab/client/src/gui/icons/arrowcircle.png"));
        jButtonRefresh.setToolTipText("Refresh");
        jButtonRefresh.setHorizontalTextPosition(SwingConstants.CENTER);
        jButtonRefresh.setVerticalTextPosition(SwingConstants.CENTER);
	jButtonRefresh.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
		    try {
			makeTaskList();
			jPanelCheckBox.repaint();
			jPanelCheckBox.validate();
		    } catch(Exception e) {}

		}
	});
        jPanel1.add(jButtonRefresh);

        if (memberId == 1) {
            jButtonAddTask = new JButton();

            jButtonAddTask.setIcon(new ImageIcon("/xml-collab/client/src/gui/icons/clipboardplus.png"));
            jButtonAddTask.setToolTipText("Add new task");
            jButtonAddTask.setHorizontalTextPosition(SwingConstants.CENTER);
            jButtonAddTask.setVerticalTextPosition(SwingConstants.CENTER);
	    final Requests req1 = this.req;
	    jButtonAddTask.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
		    try {
			new AddTask(req1);
		    } catch(Exception e) {
			 System.out.println(e);
		    }

		}
	    });
            jPanel1.add(jButtonAddTask);

	    jButtonNewMember = new JButton();

            jButtonNewMember.setIcon(new ImageIcon("/xml-collab/client/src/gui/icons/newmember.png"));
            jButtonNewMember.setToolTipText("Add new member");
            jButtonNewMember.setHorizontalTextPosition(SwingConstants.CENTER);
            jButtonNewMember.setVerticalTextPosition(SwingConstants.CENTER);
	    jButtonNewMember.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
		    try {
			new AddMember(req1);
		    } catch(Exception e) {
			 System.out.println(e);
		    }

		}
	    });
            jPanel1.add(jButtonNewMember);
        }

        f.getContentPane().add(jPanel1, BorderLayout.NORTH);

        jPanelCheckBox = new JPanel();
        jPanelCheckBox.setBorder(BorderFactory.createTitledBorder("Task list"));
        jPanelCheckBox.setLayout(new BoxLayout(jPanelCheckBox, BoxLayout.Y_AXIS));
	
        
        
        f.getContentPane().add(jPanelCheckBox, BorderLayout.CENTER);
        f.setVisible(true);
        makeTaskList();
    }
    
    
    public void makeTaskList() throws Exception{
	jPanelCheckBox.removeAll();
	JLabel waiting = new JLabel("Getting task list. Please wait...");
	jPanelCheckBox.add(waiting);

	List<Task> tasks = this.req.getTasks(memberId);

	if(tasks.size() == 0) {
	    jPanelCheckBox.removeAll();
	    JLabel jl = new JLabel("There is no tasks for you. Go out and come back later.");
	    jPanelCheckBox.add(jl);
	    return;
	}
	jPanelCheckBox.removeAll();
        for(Integer i=0; i< tasks.size(); i++) {
            final JCheckBox checkbox = new JCheckBox();
            //final JCheckBox checkbox = tasks.get(i).checkbox;
            checkbox.setText(tasks.get(i).assigment);
            jPanelCheckBox.add(checkbox);

            final Requests req1 = req;
            final Integer taskId = tasks.get(i).id;
            checkbox.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    jPanelCheckBox.remove(checkbox);
                    jPanelCheckBox.revalidate();
                    jPanelCheckBox.repaint();
                    try {
                        req1.completeTask(taskId);
                    } catch(Exception e) {System.out.println(e);}
                }
            });

        }
    }

}